package edu.pku.sei.transformation.tgg.adapter;

import edu.pku.sei.modeler.model.core.MetaNamedElement;
import edu.pku.sei.modeler.model.core.MetaReference;
import edu.pku.sei.transformation.editor.model.MetaModel;
import edu.pku.sei.transformation.editor.model.variable.ConnectionVariable;
import edu.pku.sei.transformation.tgg.rule.IEdge;
import edu.pku.sei.transformation.tgg.rule.INode;
import edu.pku.sei.transformation.tgg.rule.IPattern;

public class EdgeAdapter implements IEdge {
	ConnectionVariable element;
	public INode source;
	public INode target;
	IPattern owner;
	boolean isOpposite = false;

	public EdgeAdapter(ConnectionVariable element, IPattern owner,
			INode source, INode target) {
		this.element = element;
		this.owner = owner;
		this.source = source;
		this.target = target;
	}

	public EdgeAdapter(ConnectionVariable element, IPattern owner,
			INode source, INode target, boolean isOpposite) {
		this.element = element;
		this.owner = owner;
		this.source = source;
		this.target = target;
		this.isOpposite = isOpposite;
	}

	@Override
	public IPattern getPattern() {
		return owner;
	}

	@Override
	public INode getSource() {
		return source;
	}

	@Override
	public INode getTarget() {
		return target;
	}

	@Override
	public String getType() {
		String type = null;
		MetaReference r = (MetaReference) element.getType();
		MetaReference or = null;
		if (r != null)
			or = r.getOpposite();

		if (!isOpposite) {
			if (r != null) {
				MetaModel m = (MetaModel) r.getModel();
				return m.getFilePath() + "#//"
						+ ((MetaNamedElement) r.getContainer()).getName() + "/"
						+ element.getTargetRole();
			} else {
				return source.getType() + "/" + element.getTargetRole();
			}
		} else {
			if (or != null) {
				MetaModel m = (MetaModel) r.getModel();
				return m.getFilePath() + "#//"
						+ ((MetaNamedElement) or.getContainer()).getName()
						+ "/" + element.getSourceRole();
			} else {
				return target.getType() + "/" + element.getSourceRole();
			}
		}
	}

	@Override
	public String getID() {
		if (!isOpposite)
			return String.valueOf(element.getElementID());
		else
			return String.valueOf(element.getElementID()) + "_op";
	}

}
